{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Display a Summary of All Activities for a Given Course"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import display, Markdown\n",
    "import snakemd\n",
    "\n",
    "import fitfile\n",
    "from garmindb import GarminConnectConfigManager\n",
    "from garmindb.garmindb import GarminDb, Attributes, ActivitiesDb, Activities, ActivityLaps, ActivityRecords, StepsActivities\n",
    "\n",
    "from jupyter_funcs import format_number\n",
    "\n",
    "\n",
    "doc = snakemd.new_doc()\n",
    "\n",
    "course_id = input('Enter the id of a course to summarize')\n",
    "\n",
    "doc.add_heading(f\"Analysis for Course {course_id}\", 2)\n",
    "\n",
    "gc_config = GarminConnectConfigManager()\n",
    "db_params_dict = gc_config.get_db_params()\n",
    "\n",
    "garmin_db = GarminDb(db_params_dict)\n",
    "measurement_system = Attributes.measurements_type(garmin_db)\n",
    "unit_strings = fitfile.units.unit_strings[measurement_system]\n",
    "distance_units = unit_strings[fitfile.units.UnitTypes.distance_long]\n",
    "\n",
    "activity_db = ActivitiesDb(db_params_dict)\n",
    "\n",
    "activities = Activities.get_by_course_id(activity_db, course_id)\n",
    "\n",
    "activities_count = len(activities)\n",
    "fastest_activity = Activities.get_fastest_by_course_id(activity_db, course_id)\n",
    "slowest_activity = Activities.get_slowest_by_course_id(activity_db, course_id)\n",
    "\n",
    "doc.add_paragraph(f'{activities_count} activities using this course')\n",
    "\n",
    "def __activity_data(activity, title):\n",
    "    if activity.is_steps_activity():\n",
    "        steps_activity = StepsActivities.get(activity_db, activity.activity_id)\n",
    "        return [title, activity.start_time, activity.activity_id, activity.name, format_number(activity.distance), activity.elapsed_time, steps_activity.avg_pace, format_number(activity.avg_speed)]\n",
    "    return [title, activity.start_time, activity.activity_id, activity.name, format_number(activity.distance), activity.elapsed_time, '', format_number(activity.avg_speed)]\n",
    "\n",
    "doc.add_table(\n",
    "    ['Type', 'On', 'Id', 'Name', f'Distance ({unit_strings[fitfile.units.UnitTypes.distance_long]})', 'Time', f'Pace ({unit_strings[fitfile.units.UnitTypes.pace]})', f'Speed ({unit_strings[fitfile.units.UnitTypes.speed]})'],\n",
    "    [\n",
    "        __activity_data(activities[0], \"First\"),\n",
    "        __activity_data(activities[-1], \"Latest\"),\n",
    "        __activity_data(fastest_activity, \"Fastest\"),\n",
    "        __activity_data(slowest_activity, \"Slowest\")\n",
    "    ])\n",
    "\n",
    "display(Markdown(str(doc)))"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "d4f50e87ad7f9cd136d9d3dcf547b8236ee2585f92d0ab7c53dfb80e44e3fae9"
  },
  "kernelspec": {
   "display_name": "Python 3.9.5 64-bit ('.venv')",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
